#Replication code for 'Do Voters Discount Political Scandals Over Time?'
#Note 1: if you have any questions, please contact Miguel Pereira (m.pereira@wustl.edu)
#Note 2: The code to produce the Google trends analysis (Tables 3 and C1) is at the bottom of the script. 
library(gsheet)
library(stargazer)
library(plm)
library(interplot)
library(foreign)
library(polycor)
library(arm)
library(mice)
require(zoo)
library(car)

setwd('SET UP YOUR WORKING DIRECTORY HERE')
nbdt <- read.csv("rep_scandalsdisc.csv")


nbdt$chexp<-nbdt$chexp/1000000
nbdt$inexp<-nbdt$inexp/1000000


############################
#Figure 1 - Histogram
############################
par(mar=c(5,4,2,2))
hist(nbdt$timing2[nbdt$timing2>0&nbdt$timing2<=24], xaxt='n', 
     xlim=c(25,0),
     breaks=0:24,
     xlab="Months before the election",
     ylim=c(0,50),
     main="", cex=.8)
axis(1,at=c(0,3,6,9,12,15,18,21,24))

############################
#Figure 2 - Electoral performance by timing
############################
ns<-nbdt[nbdt$timing2>=0&nbdt$timing2<=24,]

vshares<-NULL
for (i in 0:24){
  vshares[i+1]<-mean(na.omit(ns$incpvote[ns$timing2==i]))
}

par(mar=c(5,4,2,2))
plot(1:24,vshares[-1], pch=20,# col='Gray',
     xlab='Months before the election', ylim=c(.4,.9),
     ylab='Vote Share', axes=F, xlim=c(24,0), cex=.8)
axis(1, at=rev(c(24,21,18,15,12,9,6,3,0)), cex=.8)
axis(2)
lines(lowess(1:24,vshares[-1]),type='l', lwd=2, col='Gray')
abline(vshares[1],0,lty=2, col='Gray')
legend('top', ncol=2, legend=c('Scandal', 'No Scandal'), lty=c(1,2),
       lwd=c(2,1), bty='n', cex=.8, col='Gray')


mean(na.omit(nbdt$incpvote[nbdt$anysc==0])) #Mean share for no-scandal incumbents
mean(na.omit(nbdt$incpvote[nbdt$anysc==1])) #Mean share for scandal incumbents
mean(na.omit(nbdt$incpvote[nbdt$scandalFY==1])) #First year
mean(na.omit(nbdt$incpvote[nbdt$scandalEY==1])) #Election year


############################
#TABLE 2
############################
m1<-data.frame(incpvote=nbdt$incpvote[nbdt$timing2<24&nbdt$timing2>=0],
               incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
               scandalEY=nbdt$scandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               scandalFY=nbdt$scandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               anysc=nbdt$anysc[nbdt$timing2<24&nbdt$timing2>=0],
               nchqual=nbdt$nchqual[nbdt$timing2<24&nbdt$timing2>=0],
               open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
               chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0],
               inexp=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>=0])

m1no<-m1[m1$open==0,] #restricting to non-open seats
set.seed(152)  #for replication
#Multiple imputation procedure
m1no.mids<-mice(m1no,print=FALSE)

m1no.mice<-lm.mids(incpvote~incpvotelag
                   +scandalEY
                   +scandalFY
                   +nchqual
                   +chexp
                   +inexp
                   ,m1no.mids)
round(summary(pool(m1no.mice)),3)
pool.r.squared(m1no.mice)[,1]



#To get standard error of difference between coefficients
m1noany.mice<-lm.mids(incpvote~incpvotelag
                      +scandalEY
                      +anysc
                      +nchqual
                      +chexp
                      +inexp
                      ,m1no.mids)
round(summary(pool(m1noany.mice))[3,],3)


############################
#FIGURE 3 (and Table B4)
############################
m2<-data.frame(incpvote=nbdt$incpvote[nbdt$timing2<24&nbdt$timing2>=0],
               incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
               FINscandalEY=nbdt$FINscandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               FINscandalFY=nbdt$FINscandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               CORscandalEY=nbdt$CORscandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               CORscandalFY=nbdt$CORscandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               SEXscandalEY=nbdt$SEXscandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               SEXscandalFY=nbdt$SEXscandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               POLscandalEY=nbdt$POLscandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               POLscandalFY=nbdt$POLscandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               OTscandalEY=nbdt$OTscandalEY[nbdt$timing2<24&nbdt$timing2>=0],
               OTscandalFY=nbdt$OTscandalFY[nbdt$timing2<24&nbdt$timing2>=0],
               nchqual=nbdt$nchqual[nbdt$timing2<24&nbdt$timing2>=0],
               open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
               inexp=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>=0],
               chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0],
               expdif=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>=0]-nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0])
m2no<-m2[m2$open==0,] #restricting to non-open seats
set.seed(642) #for replication
#Multiple imputation procedure
m2no.mids<-mice(m2no,print=FALSE)

nmod2no.mice<-lm.mids(incpvote~incpvotelag
                      + FINscandalEY
                      + FINscandalFY
                      + CORscandalEY
                      + CORscandalFY
                      + SEXscandalEY
                      + SEXscandalFY
                      + POLscandalEY
                      + POLscandalFY
                      + OTscandalEY
                      + OTscandalFY
                      + nchqual
                      + chexp
                      + inexp
                      ,m2no.mids)
round(summary(pool(nmod2no.mice)),3)
pool.r.squared(nmod2no.mice)

# # # # #To produce Figure 3
x<-summary(pool(nmod2no.mice))

coefplot(rev(x[3:12,1]),rev(x[3:12,2]), col.pts=c("gray","black"), 
         cex.pts=1.5, cex.var=.9, mar=c(2,6.6,4,2),oma=c(4,1,0,1), 
         lower.conf.bounds=rev(x[3:12,1])-1.96*rev(x[3:12,2]), 
         upper.conf.bounds=rev(x[3:12,1])+1.96*rev(x[3:12,2]),
         varnames=rev(c("Financial (EY)"
                        , "Financial (FY)"
                        , "Corruption (EY)"
                        , "Corruption (FY)"
                        , "Sexual (EY)"
                        , "Sexual (FY)"
                        , 'Political (EY)'
                        , "Political (FY)"
                        , "Other (EY)"
                        , "Other (FY)")), main="",
         xlim=c(-.15,.1))
mtext("Incumbent party's vote", outer=F, line=2.5, cex=1)

cofs<-x[3:12,]
for (i in 10:1){
  if (i%%2==0){
    segments(cofs[11-i,1]-1.65*cofs[11-i,2], i , cofs[11-i,1]+1.65*cofs[11-i,2], i,
             col='black', lwd=2)
  }
  if(i%%2!=0){
    segments(cofs[11-i,1]-1.65*cofs[11-i,2], i , cofs[11-i,1]+1.65*cofs[11-i,2], i,
             col='gray', lwd=2)
  }
}

par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom", ncol=3, c("Election Year", "", "First Year"), 
       xpd = TRUE, #horiz = TRUE, #inset = c(0,0),
       bty = "n", lty=c(1,NA,1), pch=c(20,NA,20), pt.cex=1.2,
       col = c("black","white","gray"), cex = .9)

#Pulling pvals of linear hypotheses from mice object
pvals_sex<-NULL
pvals_sex_fin<-NULL
pvals_sex_co<-NULL
for (i in 1:5){
  m2_imp<-nmod2no.mice$analyses[[i]]
  linear_hyp<-linearHypothesis(m2_imp, 'SEXscandalEY = SEXscandalFY')
  pvals_sex[i]<-linear_hyp$`Pr(>F)`[2]
  linear_hyp2<-linearHypothesis(m2_imp, 'SEXscandalFY = FINscandalFY')
  pvals_sex_fin[i]<-linear_hyp2$`Pr(>F)`[2]
  linear_hyp3<-linearHypothesis(m2_imp, 'SEXscandalFY = CORscandalFY')
  pvals_sex_co[i]<-linear_hyp3$`Pr(>F)`[2]
}
mean(pvals_sex) #sex scandal EY = sex scandal FY
mean(pvals_sex_fin) #sex scandal FY > financial scandal  FY
mean(pvals_sex_co) #sex scandal FY > corruption scandal FY


############################
#TABLE B1 - Main results with listwise deletion
############################
comps<-complete.cases(cbind(nbdt$incpvote,nbdt$incpvotelag,nbdt$scandalEY,nbdt$scandalFY,nbdt$nchqual,nbdt$open,
                            nbdt$chexp,nbdt$inexp))

nmod1Y<-lm(incpvote~incpvotelag
           +scandalEY
           +scandalFY
           +nchqual
           +chexp
           +inexp
           ,nbdt[comps & nbdt$timing2<24&nbdt$timing2>=0&nbdt$open==0,])
summary(nmod1Y)



###############################
#TABLE B2 - Results by semester
###############################
m4<-data.frame(incpvote=nbdt$incpvote[nbdt$timing2<24&nbdt$timing2>=0],
               incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq1=nbdt$scandalq1[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq2=nbdt$scandalq2[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq3=nbdt$scandalq3[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq4=nbdt$scandalq4[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq5=nbdt$scandalq5[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq6=nbdt$scandalq6[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq7=nbdt$scandalq7[nbdt$timing2<24&nbdt$timing2>=0],
               scandalq8=nbdt$scandalq8[nbdt$timing2<24&nbdt$timing2>=0],
               nchqual=nbdt$chqual[nbdt$timing2<24&nbdt$timing2>=0],
               open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
               timing2=nbdt$timing2[nbdt$timing2<24&nbdt$timing2>=0],
               chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0],
               inexp=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>=0])
m4no<-m4[m4$open==0,]
set.seed(7654)
m4no.mids<-mice(m4no,print=FALSE)
m4no.mice<-lm.mids(incpvote~incpvotelag
                   +scandalq1
                   +scandalq2
                   +scandalq3
                   +scandalq4
                   +scandalq5
                   +scandalq6
                   +scandalq7
                   +scandalq8
                   +nchqual
                   +chexp
                   +inexp
                   ,m4no.mids)
round(summary(pool(m4no.mice)),3)  
pool.r.squared(m4no.mice)



############################
#TABLE B3 - Results with continuous measure of timing
############################
m1_cont<-data.frame(incpvote=nbdt$incpvote[nbdt$timing2<24&nbdt$timing2>0],
                    incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>0],
                    time_inv=25-nbdt$timing2[nbdt$timing2<24&nbdt$timing2>0],
                    nchqual=nbdt$nchqual[nbdt$timing2<24&nbdt$timing2>0],
                    chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>0],
                    inexp=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>0],
                    open=nbdt$open[nbdt$timing2<24&nbdt$timing2>0])
m1_cont$timing<-25-m1_cont$time_inv
set.seed(152)  #for replication
#Multiple imputation procedure
m1.mids_cont<-mice(m1_cont,print=FALSE)

m1.mice_cont<-lm.mids(incpvote~incpvotelag
                      +timing
                      +nchqual
                      +chexp
                      +inexp
                      ,m1.mids_cont)
round(summary(pool(m1.mice_cont)),3) 
pool.r.squared(m1.mice_cont)


############################
#TABLE B5 - Excluding primary months
############################
m1_rob<-data.frame(incpvote=nbdt$incpvote[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   scandalEY=nbdt$scandalEY[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   scandalFY=nbdt$scandalFY[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   nchqual=nbdt$nchqual[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   inexp=nbdt$inexp[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T],
                   timing2=nbdt$timing2[nbdt$timing2<24&nbdt$timing2>=0& nbdt$timing2%in%c(9,10)!=T])
m1no_rob<-m1_rob[m1_rob$open==0,]
set.seed(154)
m1no_rob.mids<-mice(m1no_rob,print=FALSE)

m1no_rob.mice<-lm.mids(incpvote~incpvotelag
                       +scandalEY
                       +scandalFY
                       +nchqual
                       +chexp
                       +inexp
                       ,m1no_rob.mids)
round(summary(pool(m1no_rob.mice)),3)
pool.r.squared(m1no_rob.mice)


############################
#TABLE B6 - Scandal timing by type of scandal
############################
set.seed(5432)
m6.ap<-data.frame(timing2=nbdt$timing2[nbdt$timing2<24&nbdt$timing2>0],
                  incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>0],
                  sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>0],
                  corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>0],
                  finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>0],
                  polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>0],
                  altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>0],
                  open=nbdt$open[nbdt$timing2<24&nbdt$timing2>0])
m6no.ap<-m6.ap[m6.ap$open==0,]
m6no.ap.mids<-mice(m6no.ap,print=FALSE)
m6no<-lm.mids(timing2~incpvotelag+corruption+finscandal+polscandal+altscandal
              ,m6no.ap.mids)
round(summary(pool(m6no)),3)
pool.r.squared(m6no)


############################
#TABLE B7 - Challenger type and open seats, by type of scandal
############################
### COLUMN 1
m5.ap<-data.frame(chqual=nbdt$chqual[nbdt$timing2<24&nbdt$timing2>=0],
                  open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
                  incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
                  sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>=0],
                  finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>=0])
m5no.ap<-m5.ap[m5.ap$open==0,]
set.seed(1252)
m5no.ap.mids<-mice(m5no.ap,print=FALSE)
m5.1no<-glm.mids(chqual~incpvotelag
                 +sexscandal+corruption+finscandal+polscandal+altscandal
                 ,family=('binomial'),m5no.ap.mids)
round(summary(pool(m5.1no)) ,3)

#Differences in coefficients
temp<-glm(chqual~incpvotelag
                 +sexscandal+corruption+finscandal+polscandal+altscandal
                 ,family=('binomial'),m5no.ap)
summary(temp)
linearHypothesis(temp,'sexscandal = corruption')
linearHypothesis(temp,'sexscandal = finscandal')
linearHypothesis(temp,'sexscandal = polscandal')
linearHypothesis(temp,'sexscandal = altscandal')


### COLUMN 2
m5.ap.short<-data.frame(chqual=nbdt$chqual[nbdt$timing2<24&nbdt$timing2>0],
                        open=nbdt$open[nbdt$timing2<24&nbdt$timing2>0],
                        incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>0],
                        sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>0],
                        corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>0],
                        finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>0],
                        polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>0],
                        altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>0])
m5no.ap.short<-m5.ap.short[m5.ap.short$open==0,]
set.seed(1252)
m5no.ap.mids.short<-mice(m5no.ap.short,print=FALSE)

m5.2no<-glm.mids(chqual~incpvotelag
                 +corruption+finscandal+polscandal+altscandal
                 ,family=('binomial'),m5no.ap.mids.short)
round(summary(pool(m5.2no)),3) 



#####With expenditures as the outcome
### COLUMN 3
m5.exp<-data.frame(chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>=0],
                  open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
                  incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
                  sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>=0],
                  finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>=0],
                  altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>=0])
m5no.exp<-m5.exp[m5.exp$open==0,]
set.seed(1252)
m5no.exp.mids<-mice(m5no.exp,print=FALSE)
m5.1no<-lm.mids(chexp ~ incpvotelag
                 +sexscandal+corruption+finscandal+polscandal+altscandal
                 ,m5no.exp.mids)
round(summary(pool(m5.1no)),3)
pool.r.squared(m5.1no)

#Differences in coefficients
temp<-lm(chexp ~ incpvotelag
          +sexscandal+corruption+finscandal+polscandal+altscandal
          ,m5no.exp)
summary(temp)
linearHypothesis(temp,'sexscandal = corruption')
linearHypothesis(temp,'sexscandal = finscandal')
linearHypothesis(temp,'sexscandal = polscandal')
linearHypothesis(temp,'sexscandal = altscandal')


### COLUMN 4
m5.exp.short<-data.frame(chexp=nbdt$chexp[nbdt$timing2<24&nbdt$timing2>0],
                        open=nbdt$open[nbdt$timing2<24&nbdt$timing2>0],
                        incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>0],
                        sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>0],
                        corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>0],
                        finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>0],
                        polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>0],
                        altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>0])
m5no.exp.short<-m5.exp.short[m5.exp.short$open==0,]
set.seed(1252)
m5no.exp.mids.short<-mice(m5no.exp.short,print=FALSE)

m5.2no<-lm.mids(chexp~incpvotelag
                 +corruption+finscandal+polscandal+altscandal
                 ,m5no.exp.mids.short)
round(summary(pool(m5.2no)) ,3)
pool.r.squared(m5.2no)



#####With open seat as the outcome
### COLUMN 5
m5.op<-data.frame(open=nbdt$open[nbdt$timing2<24&nbdt$timing2>=0],
                   incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>=0],
                   sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>=0],
                   corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>=0],
                   finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>=0],
                   polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>=0],
                   altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>=0])
set.seed(1252)
m5op.mids<-mice(m5.op,print=FALSE)
m5.3<-glm.mids(open ~ incpvotelag
                +sexscandal+corruption+finscandal+polscandal+altscandal
                ,family='binomial',m5op.mids)
round(summary(pool(m5.3)),3)

#to get differences in coefficients
temp<-glm(open~incpvotelag
          +sexscandal+corruption+finscandal+polscandal+altscandal
          ,family=('binomial'),m5.op)
summary(temp)
linearHypothesis(temp,'sexscandal = corruption')
linearHypothesis(temp,'sexscandal = finscandal')
linearHypothesis(temp,'sexscandal = polscandal')
linearHypothesis(temp,'sexscandal = altscandal')


### COLUMN 6
m5.op.short<-data.frame(open=nbdt$open[nbdt$timing2<24&nbdt$timing2>0],
                  incpvotelag=nbdt$incpvotelag[nbdt$timing2<24&nbdt$timing2>0],
                  sexscandal=nbdt$sexscandal[nbdt$timing2<24&nbdt$timing2>0],
                  corruption=nbdt$corruption[nbdt$timing2<24&nbdt$timing2>0],
                  finscandal=nbdt$finscandal[nbdt$timing2<24&nbdt$timing2>0],
                  polscandal=nbdt$polscandal[nbdt$timing2<24&nbdt$timing2>0],
                  altscandal=nbdt$altscandal[nbdt$timing2<24&nbdt$timing2>0])
set.seed(1252)
m5op.short.mids<-mice(m5.op.short,print=FALSE)
m5.3short<-glm.mids(open ~ incpvotelag
               +corruption+finscandal+polscandal+altscandal
               ,family='binomial',m5op.short.mids)
round(summary(pool(m5.3short)),3)



# # # # # # # # # # # # # # # #
# # TABLE B8 - Main results with open and occupied seats
# # # # # # # # # # # # # # # #
set.seed(152)  #for replication
m1.mids<-mice(m1,print=FALSE)

### COLUMN 1
m1.mice<-lm.mids(incpvote~incpvotelag
                 +scandalEY
                 +scandalFY
                 +nchqual
                 +chexp
                 +inexp
                 +open
                 ,m1.mids)
round(summary(pool(m1.mice)),3)
pool.r.squared(m1.mice)


set.seed(81443)
#Multiple imputation procedure
m2.mids<-mice(m2,print=FALSE)

### COLUMN2
nmod2.mice<-lm.mids(incpvote~incpvotelag
                    + FINscandalEY
                    + FINscandalFY
                    + CORscandalEY
                    + CORscandalFY
                    + SEXscandalEY
                    + SEXscandalFY
                    + POLscandalEY
                    + POLscandalFY
                    + OTscandalEY
                    + OTscandalFY
                    + nchqual
                    + open
                    + inexp
                    + chexp
                    ,m2.mids)
round(summary(pool(nmod2.mice)),3)  
pool.r.squared(nmod2.mice)



# # # # # # # # # # # # # # # #
# # GOOGLE TRENDS ANALYSIS
# # # # # # # # # # # # # # # #
combs<-read.csv('trends.csv')

############################
#TABLE C1 - Relative volumes of interest
############################
comps<-combs[combs$SexVS==1,]
comps<-comps[ order(comps[,6]), ]
comps$congress<-ifelse(comps$Year==2006,109,
                       ifelse(comps$Year==2008,110,111))
ncomps<-comps[,c('Rep2', 'Rep1', 'State', 'congress', 'OverallMax2', 'OverallMax1', 'MaxMonth2', 'MaxMonth1')]
ncomps

############################
#TABLE 3 - Differences in state-level relative volumes of interest
############################
summary(lm(MaxDiff_2~fin_sex+pol_sex+sex_other+fin_other+fin_pol+pol_pol+State,combs))


